
#include <stdafx.h>
#include "TargetContainer.h"

//#define TEST_DX9

#if defined(TEST_DX9)
#include "alkes/alkesdx9.h"
typedef alkes::DirectXGraphics TestGraphics;
#else
#include "alkes/alkesogl.h"
typedef alkes::WGLGraphics TestGraphics;
#endif

using namespace alkes;

#define FILEPATH_PNG    "../data/pencil.png"
//#define FILEPATH_PNG    "../data/grad_gray.png"
#define FILEPATH_JPEG   "../data/image.jpg"
#define FILEPATH_MASK   "../data/mask.png"

TargetContainer::TargetContainer()
: graphics_(0)
, screen_test_(0)
{
}

TargetContainer::~TargetContainer()
{
    if (screen_test_)
        delete screen_test_;
}

bool TargetContainer::flip()
{
    graphics_->flip();
    return true;
}

void TargetContainer::resize(uint16_t width, uint16_t height)
{
    graphics_->resize(width, height);
}

bool TargetContainer::init(HWND hwnd)
{
    intrusive_ptr<IGraphics3D> temp(TestGraphics::create(hwnd));
    if (0 == temp)
        return false;

    graphics_ = GfxFilterGraphics::create(temp);

    return 0 != graphics_;
}

bool TargetContainer::render(TestCaseFunc testcase)
{
    intrusive_ptr<IRenderTarget2D> target(graphics_->createDefaultRenderTarget2D());
    intrusive_ptr<DecoderFactory> factory(graphics_->createDecoderFactory());

    RenderTarget2DTestDriver driver(target, factory);

    (this->*testcase)(driver);

    return true;
}

#define DEFINE_IMPLEMENT(TestDriver, Function)                          \
    void TargetContainer::TestDriver##_##Function(TestDriver& driver) { \
        driver.test##Function();                                        \
    }

DEFINE_IMPLEMENT(RenderTarget2DTestDriver, Snapshot)
DEFINE_IMPLEMENT(RenderTarget2DTestDriver, Draw2D)
DEFINE_IMPLEMENT(RenderTarget2DTestDriver, Scissor)
DEFINE_IMPLEMENT(RenderTarget2DTestDriver, AlphaBlend)
DEFINE_IMPLEMENT(RenderTarget2DTestDriver, Brush)
DEFINE_IMPLEMENT(RenderTarget2DTestDriver, Fill)
DEFINE_IMPLEMENT(RenderTarget2DTestDriver, FillRectangle)
DEFINE_IMPLEMENT(RenderTarget2DTestDriver, DrawRectangle)
DEFINE_IMPLEMENT(RenderTarget2DTestDriver, DrawLine)
DEFINE_IMPLEMENT(RenderTarget2DTestDriver, DrawPolyline)
DEFINE_IMPLEMENT(RenderTarget2DTestDriver, Transform)

void TargetContainer::RenderTarget2DTestDriver_DrawImagePng(alkes::RenderTarget2DTestDriver& driver)
{
    driver.testDrawImage(FILEPATH_PNG/*, FILEPATH_MASK*/);
}

void TargetContainer::RenderTarget2DTestDriver_DrawImageJpeg(alkes::RenderTarget2DTestDriver& driver)
{
    driver.testDrawImage(FILEPATH_JPEG);
}

void TargetContainer::RenderTarget2DTestDriver_Translate(alkes::RenderTarget2DTestDriver& driver)
{
    driver.testTranslate(FILEPATH_PNG);
}

void TargetContainer::RenderTarget2DTestDriver_Scale(alkes::RenderTarget2DTestDriver& driver)
{
    driver.testScale(FILEPATH_PNG);
}

void TargetContainer::RenderTarget2DTestDriver_Rotate(alkes::RenderTarget2DTestDriver& driver)
{
    driver.testRotate(FILEPATH_PNG);
}

void TargetContainer::RenderTarget2DTestDriver_Skew(alkes::RenderTarget2DTestDriver& driver)
{
    driver.testSkew(FILEPATH_PNG);
}



bool TargetContainer::render(const alkes::Rect* dirty_rect)
{
    if (!screen_test_)
        return false;

    screen_test_->render(dirty_rect);

    return true;
}

void TargetContainer::update()
{
    if (!screen_test_)
        return ;

    screen_test_->update();
}

void TargetContainer::Screen2DTestDriver_beginTestCase1()
{
    if (screen_test_)
        delete screen_test_;

    screen_test_ = new Screen2DTestDriver(graphics_);

    screen_test_->beginTestCase1();
}

